<?php
if(!defined('BZ_XTBLOG')){
    exit('Access Denied!');
}
 class mysql{
     private $host;
     private $name;
     private $pass;
     private $table;
     private $ut;
     private $qhbm;
     function __construct($host,$name,$pass,$table,$ut,$qhbm){
     	 $this->host=$host;
     	 $this->name=$name;
     	 $this->pass=$pass;
     	 $this->table=$table;
     	 $this->ut=$ut;
     	 $this->qhbm=$qhbm;
     	 $this->connect();
     	 $this->close_connect();
     }
     function connect(){
        @$link=mysql_connect($this->host,$this->name,$this->pass) or die (
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n
            <!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.0//EN\" \"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">\n
            <html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n
            <head>\n
            <title>数据库连接失败！</title>\r\n
            </head>\r\n
            <body>\r\n
            数据库连接失败！<br />
            </body>
            </html>");
        mysql_select_db($this->table,$link) or die(
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n
            <!DOCTYPE html PUBLIC \"-//WAPFORUM//DTD XHTML Mobile 1.0//EN\" \"http://www.wapforum.org/DTD/xhtml-mobile10.dtd\">\n
            <html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n
            <head>\n
            <title>数据库不存在！</title>\r\n
            </head>\r\n
            <body>\r\n
            没有该数据库！<br />
            </body>
            </html>");
        mysql_query("SET NAMES '$this->ut'");
     }
    function show($message,$sql){
		   global $Bingz_url_upgrade,$httpurl,$url_b,$serverip,$bzxtvbb;
		     $sql='('.$sql.')  Error=('.mysql_error().')';
		     Bingz_Error_Reporting('Bingz:-mysql(sos)',$sql); 
		     $sql=urlencode($sql);
		     $url_bb=urlencode($url_b);
		     $url=urlencode($url);
		     $ip=urlencode($serverip);

	       $cn=curl_init();
         curl_setopt($cn,CURLOPT_URL,''.$Bingz_url_upgrade.'/Bingz_mysql.php?sql='.$sql.'&url='.$httpurl.'&url_b='.$url_bb.'&ip='.$serverip.'&bvb='.$bzxtvbb.'');
         curl_setopt($cn,CURLOPT_RETURNTRANSFER,1);
         $content=curl_exec($cn);
         curl_close($cn);
         if(!$content){
        	   $content=@file_get_contents(''.$Bingz_url_upgrade.'/Bingz_mysql.php?sql='.$sql.'&url='.$httpurl.'&url_b='.$url_bb.'&ip='.$serverip.'&bvb='.$bzxtvbb.'');
         }
	  }
	  
   function close_connect(){
	   if(!$this->connect){
	   	  $this->connect();
	   }
   }
  
 public function mql_query($sql) {
 	   $this->close_connect();
	   if(!($query = mysql_query($sql))) $this->show('mysql:',$sql);
	   //Bingz_Error_Reporting($sql,'mmysql:');
	   //$this->show('mysql:',$sql);
		 return $query;
 }
	 function jzmysql_condition($name) {
      $pass=substr(trim($name),0,5);
      if(!$pass or in_array($pass, array('where','WHERE'))) {
      	$name=" $name";
      }else{
          if(!in_array($pass, array('LIMIT','ORDER','limit','order'))) {
             $name="WHERE $name";
          }else{
             $name=" $name";
          }
      }
	 	  return $name;
	 }
	 function insert_id(){
	 	 return mysql_insert_id();
	 }
 public function query($table,$condition,$lx='*') {
	 	$table="`{$this->qhbm}$table`";
	 	$condition=$this->jzmysql_condition($condition);
		if(empty($table) or empty($condition)){
			 //$aa='-1';
		}else{
		   if(!$lx){$lx='*';}
 	     $sql = "SELECT $lx FROM $table $condition";
	     $query =$this->mql_query($sql);
	  }
		return $query;
	}
  public function fetch_array($table) {
	 	$table="`{$this->qhbm}$table`";
		if(empty($table)){
			 //$aa='-1';
		}else{
		  $aa = mysql_fetch_array($table);
		}
		return $aa;
	}
 public function get_one($table,$condition) {
	 	$table="`{$this->qhbm}$table`";
	 	$condition=$this->jzmysql_condition($condition);
		if(empty($table) or empty($condition)){
			 //$aa='-1';
		}else{
			$sql = "SELECT * FROM $table $condition";
      $query = $this->mql_query($sql);
		  $aa = mysql_fetch_array($query);
		}
		return $aa;
 }
  public function get_all($table,$condition) {
	 	$table="`{$this->qhbm}$table`";
	 	$condition=$this->jzmysql_condition($condition);
		if(empty($table) or empty($condition)){
			 //$aa='-1';
		}else{
			$sql = "SELECT * FROM $table $condition";
      $query = $this->mql_query($sql);
  		$i = 0;
		  $aa = array();
		  while($row = mysql_fetch_array($query)) {
			   $aa[]=$row;
			   $i++;
		  }
		}
		return $aa;
	}
	 public function insert($table,$dataArray,$condition='') {
	 	$table="`{$this->qhbm}$table`";
		if(empty($table) or empty($dataArray) ){
			 $aa='';
		}else if(!is_array($dataArray) && $condition ){
		   $sql   = "INSERT INTO $table ($dataArray) VALUES ($condition) ";
		   if($this->mql_query($sql)){ $aa=mysql_insert_id();; }
		}else{
   		 $field = "";
		   $value = "";
		   while( @list($key,$val) = @each($dataArray)) {
		   	  $key=trim($key);
		    	$field.="`$key`,";
		   	  $value.="'$val',";
		   }
		   $value=str_replace("'NULL'",'NULL',$value);
		   $field= @substr($field,0,-1);
		   $value= @substr($value,0,-1);
		   $sql  = "INSERT INTO $table ($field) VALUES ($value)";
		   if($this->mql_query($sql)){ $aa=mysql_insert_id(); }
	  }
		return $aa;
	}
	 public function update($table,$dataArray,$condition) {
	 	$table="`{$this->qhbm}$table`";
	 	$condition=$this->jzmysql_condition($condition);
		if(empty($table) or empty($dataArray) or empty($condition)){
			 $aa='-1';
		}else if(!is_array($dataArray) && $condition ){
		   $sql   = "UPDATE $table SET $dataArray $condition";
		   if($this->mql_query($sql)){ $aa='1'; }
		}else{
		   $value = "";
		   while( @list($key,$val) = @each($dataArray)) {
		   	  $key=trim($key);
		   	  $nammp=substr(trim($val),0,1);
		   	  if(in_array($nammp, array('-','+')) ) {
		   	     $vala='`'.$key.'`'.$val;
		   	  }else{
		   	  	 $vala="'$val'";
		   	  }
		      $value .= "`$key` = $vala,";
		   }
		   $value = @substr( $value,0,-1);
		   $sql    = "UPDATE $table SET $value $condition";
		   if($this->mql_query($sql)){ $aa='1'; }
	  }
		return $aa;
	 }
	 public function delete($table,$condition) {
	 	$table="{$this->qhbm}$table";
	 	$condition=$this->jzmysql_condition($condition);
		if(empty($table) or empty($condition)){
			 //$aa='-1';
		}else if(@$this->mql_query("delete from $table $condition")){
			 $aa='1';
		}
		return $aa;
	}
	public function num_rows($table,$condition,$lx='*'){
	 	$table="{$this->qhbm}$table";
	 	$condition=$this->jzmysql_condition($condition);
		if(empty($table) or empty($condition)){
			 //$aa='-1';
		}else{
			 $aa=mysql_num_rows($this->mql_query("SELECT $lx FROM $table $condition"));
    }
		return $aa;
	}
   function free_result($table){
	      mysql_free_result($table);
   }
   function close(){
	   if($this->connect){
	      mysql_close($this->connect);
	   }
   }
 }
$db =  new mysql($_config['mysql']['host'],$_config['mysql']['user'],$_config['mysql']['pass'],$_config['mysql']['database'],'utf8',$_config['mysql']['qianh']);


function Bingz_config($skey,$svalue='',$post=''){
	  global $db,$Bingz_fication;
	  $config=$db->get_one('config'," `skey` = '$skey'",'`svalue`');
	  if($post == 'post'){
	  	  if($config){
	  	  	 $db->update('config',array('svalue' => $svalue ),"`skey` ='$skey'");
	  	 	} else {
	  	 		 $db->insert('config',array('skey' => $skey, 'svalue' => $svalue));
	  	 	}
	  	 $confign=$skey;
	  } else {
	     $confign=$config[svalue];
	  }
   return $confign;
}
$Bingz_Error=Bingz_config('system_config_Error');
function Bingz_Error_Reporting($content,$name,$txtname=''){
	   global $Bingz_Error,$url_b,$ip,$db;
     if(!file_exists(MBLOG_ROOT.''.$Bingz_Error.'/')){
			     @mkdir(MBLOG_ROOT."$Bingz_Error/",0777,true);
           $fp=fopen(MBLOG_ROOT."$Bingz_Error/index.php","w");
           $write=fputs($fp,"");
           fclose($fp);
	   }
	   if($txtname){
	   	  $txtname=$txtname.date("Y-m-d");
	   }else{
        $txtname=date("Y-m-d");
     }
     $time=date("Y-m-d H:i:s");
     $mysql ="$time ($ip) $url_b  ($name) 提示：$content \r\n";
	   $fp   =fopen(MBLOG_ROOT.$Bingz_Error.'/'.$txtname.'.txt','a+');
	   fwrite($fp,$mysql);
	   fclose($fp);	
}

?>
